const app = getApp()
// const user = wx.getStorageSync("user");
var user_token = wx.getStorageSync('user_token');
const base_url = app.globalData.base_url;
var OPEN_ID = ''//储存获取到openid  
var SESSION_KEY = ''//储存获取到session_key  
var carphone = 0;
var kfrx = 0;
var id = 0;
const util = require('../../utils/util.js');
Page({

  /**
  * 页面的初始数据
  */
  data: {
    carinfo: {},
    carphone: 0,
    hide_ad:false,
    movies: []    
  },
  onLoad: function (options) {
    var that = this;
    id = decodeURIComponent(options.id);
    var jump_type = decodeURIComponent(options.jump_type);

    if (jump_type=='undefined'){
      jump_type = 0;
    }

    var regNum = new RegExp('[0-9]', 'g');
    if (!regNum.exec(id)) {
      id = id.substr(52);
    }
    this.setData({
      q: id
    })
    this.init(id,jump_type)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  callCarPhone: function (event) {
    // wx.redirectTo({
    //   url: '../toPage/index?q=' +encodeURIComponent('https://lc.feiyouce.com/index.php?s=/Qcode/index/id/1')
    // })
    wx.makePhoneCall({
      phoneNumber: carphone
    })
  },
  toAd:function(e){
    var adid = e.currentTarget.dataset.adid
    console.log(adid);
    wx.navigateTo({
      url: '../ad/index?id=' + adid
    })
  },
  callPhone: function (event) {
    wx.makePhoneCall({
      phoneNumber: kfrx
    })
  },
  getInfo: function (event) {
    wx.navigateTo({
      url: '../detail/index?id=' + id
    })
  },

  init: function (id,jump_type) {
    // id = 2
    // var user_token ='c9eb14a86f7328ca447bf949949eb574'
    wx.showLoading({
      title: '加载中',
      success: () => {
        wx.request({
          url: base_url + '/Qcode/index',
          method: 'POST',
          data: {
            // 'user_token': user_token,
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'id': id
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            console.log(res)
            if (res.data.success == '1') {
              if (res.data.code == '0' || (res.data.code == '801' && jump_type == 1)) {

                var nick_name = ''
                if (res.data.data.real_name==''){
                  nick_name = res.data.data.wx_nick_name;
                }else{
                  nick_name = res.data.data.real_name;
                }
                // console.log(util.strchangdu(res.data.data.wx_nick_name));
                var len = 0;
                for (var i = 0; i < nick_name.length; i++) {
                  var c = nick_name.charCodeAt(i);
                  //单字节加1 
                  if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
                    len++;
                  }
                  else {
                    len += 2;
                  }
                }

                if (len > 18) {
                  nick_name = nick_name.substr(0, 18);
                  nick_name = nick_name + '...';
                }
                var wx_number = res.data.data.wx_number
                if (wx_number!=null&&wx_number.length>20){
                  wx_number = wx_number.substr(0, 20);
                  wx_number = wx_number + '...';
                }
                res.data.data.wx_nick_name = nick_name;
                res.data.data.wx_number = wx_number;

                if(res.data.data.hide_ad=='0'){
                  this.setData({
                    hide_ad: true
                  })
                }

                this.setData({
                  carinfo: res.data.data,
                  movies: res.data.data.ad,
                  carphone: res.data.data.phone
                })
                carphone = res.data.data.phone
                wx.hideLoading();
              } else if (res.data.code == '800') {
                wx.redirectTo({
                  url: '../bindPhone/index?id=' + id
                })
              } else if (res.data.code == '801') {
                  wx.redirectTo({
                    url: '../myInfo/index'
                  })
              }
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    })

  },
})
